package com.aopeng.controller;

import com.aopeng.entity.AopengFriendLink;
import com.aopeng.mapper.AopengFriendLinkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 谢辉
 * @description 权限管理控制器
 * @data 2020/7/17
 */
@RequestMapping("/authority")
@Controller
public class AuthorityController {

    @Autowired
    private AopengFriendLinkMapper friendLinkMapper;

    /**
    * 查看所有友链
    * @author   谢辉
    * @date      2020/7/17 13:57
    */
    @GetMapping
    public String friendPage(Model model){
        model.addAttribute("link",friendLinkMapper.selectList(null));
        return "admin/friend";
    }

    /**
     * 友链表单新增跳转
     * @author   谢辉
     * @date      2020/7/17 13:57
     */
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public String friendLinkPage(){
        return "admin/friend-input";
    }

    /**
     * 新增友链
     * @author   谢辉
     * @date      2020/7/17 13:57
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addFriendLink(AopengFriendLink aopengFriendLink){
        aopengFriendLink.setIsDelete(1);
        aopengFriendLink.setCreator(1L);
        aopengFriendLink.setUpdater(1L);
        friendLinkMapper.insert(aopengFriendLink);
        return "redirect:/authority";
    }

    /**
    * 根据id查询出一条友链
    * @author   谢辉
    * @date      2020/7/17 15:46
    */
    @RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
    public String friendLinkById(@PathVariable("id")Long id,Model model){
        model.addAttribute("friend",friendLinkMapper.selectById(id));
        return "admin/friend-update";
    }

    /**
     * 修改友链
     * @author   谢辉
     * @date      2020/7/17 13:57
     */
    @RequestMapping(value = "/update/input",method = RequestMethod.POST)
    public String updateFriendLink(@RequestParam("id")Long id, @RequestParam("linkName")String linkName,
                                   @RequestParam("linkAddress")String linkAddress,
                                   HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        AopengFriendLink friendLink = new AopengFriendLink(id,linkAddress,linkName);
        friendLinkMapper.updateById(friendLink);
        return "redirect:/authority";
    }

    /**
     * 删除友链
     * @author   谢辉
     * @date      2020/7/17 13:57
     */
    @RequestMapping(value = "/delete/{id}/input",method = RequestMethod.GET)
    public String delFriendLink(@PathVariable("id")Long id){
        friendLinkMapper.deleteById(id);
        return "redirect:/authority";
    }
}
